﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Stala
{
	class SimpleVersionDownloader : IVersionDownloader
	{
		Uri downloadSource;
		Uri destination;

		public SimpleVersionDownloader(Uri downloadSource, Uri destination)
		{
			this.downloadSource = downloadSource;
			this.destination = destination;
		}

		public Uri DownloadSource
		{
			get { return downloadSource; }
			set { downloadSource = value; }
		}

		public Uri Destination
		{
			get { return destination; }
			set { destination = value; }
		}

		public void Download()
		{
			try
			{
				var webClient = new WebClient();
				webClient.DownloadFile(downloadSource.AbsoluteUri, destination.AbsolutePath);
			}
			catch(Exception ex)
			{
				if (DownloadFailed != null)
					DownloadFailed(ex.Message);
				else
					throw;
			}
			
			if (DownloadCompleted != null)
				DownloadCompleted();
		}

		public event Action DownloadCompleted;

		public event Action<string> DownloadFailed;
	}
}
